package com.neuedu.controller;

import com.neuedu.util.CommonResult;
import com.neuedu.util.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.xml.stream.events.Comment;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/upload")
public class FileuploadController {


    //上传单个文件，返回下载路径
    @PostMapping("/upfile")
    CommonResult getfilePath(MultipartFile file) throws IOException {
        //返回上传文件到
        String uri = FileUtils.uploadFile(file);
        if(uri != null){
            return CommonResult.success(uri);
        }else {
            return CommonResult.failed("上传文件失败");
        }
    }

    //上传多个文件，返回多个文件的下载路径
    @PostMapping("/upfiles")
    CommonResult getfilePaths(MultipartFile[] file) throws IOException {
        //返回上传文件到
        List uri = FileUtils.uploadFiles(file);
        if(uri != null){
            return CommonResult.success(uri);
        }else {
            return CommonResult.failed("上传文件失败");
        }
    }


}
